﻿Public Class TMyProcessList
    Implements System.IDisposable
    Private _List As New List(Of TMyProcess)
    Private _User As String
    Private _Host As String
    Private _DB As String
    Private _MyQuery As New TMyQuery
    Private _MyProcessID As Integer
    Private _IgnoreMySelf As Boolean

    Public Sub New(ByVal AConnection As TMySession, ByVal AIgnoreMySelf As Boolean)
        _MyQuery.Session = AConnection
        _IgnoreMySelf = AIgnoreMySelf
        If AIgnoreMySelf Then
            SetMyProcessID()
        End If
        InitVariables()
        InitSQL()
    End Sub

    Public ReadOnly Property MyProcessID() As Integer
        Get
            Return _MyProcessID
        End Get
    End Property

    Private Sub SetMyProcessID()
        _MyQuery.Clear()
        _MyQuery.SQL.AppendLine("select")
        _MyQuery.SQL.AppendLine(" variable_value")
        _MyQuery.SQL.AppendLine("from")
        _MyQuery.SQL.AppendLine(" information_schema.session_variables")
        _MyQuery.SQL.AppendLine("where")
        _MyQuery.SQL.AppendLine("variable_name like '%PSEUDO_THREAD_ID%'")
        _MyQuery.Execute()
        _MyProcessID = _MyQuery.FieldAsInteger("variable_value")
        _MyQuery.Close()
    End Sub

    Private Sub InitVariables()
        _DB = String.Empty
        _Host = String.Empty
        _User = String.Empty
    End Sub

    Public ReadOnly Property Count() As Integer
        Get
            Return _List.Count
        End Get
    End Property
    Default Public ReadOnly Property Items(ByVal Index As Integer) As TMyProcess
        Get
            Return _List(Index)
        End Get
    End Property
    Public Property User() As String
        Get
            Return _User
        End Get
        Set(ByVal value As String)
            InitVariables()
            _User = value
            InitSQL()
        End Set
    End Property
    Public Property DB() As String
        Get
            Return _DB
        End Get
        Set(ByVal value As String)
            InitVariables()
            _DB = value
            InitSQL()
        End Set
    End Property
    Public Property Host() As String
        Get
            Return _Host
        End Get
        Set(ByVal value As String)
            InitVariables()
            _Host = value
            InitSQL()
        End Set
    End Property

    Private Sub InitSQL()
        _MyQuery.Clear()
        _MyQuery.SQL.AppendLine("select")
        _MyQuery.SQL.AppendLine(" ID")
        _MyQuery.SQL.AppendLine(",USER")
        _MyQuery.SQL.AppendLine(",HOST")
        _MyQuery.SQL.AppendLine(",DB")
        _MyQuery.SQL.AppendLine(",COMMAND")
        _MyQuery.SQL.AppendLine(",TIME")
        _MyQuery.SQL.AppendLine(",STATE")
        _MyQuery.SQL.AppendLine(",INFO")
        _MyQuery.SQL.AppendLine("from")
        _MyQuery.SQL.AppendLine(" information_schema.processlist")
        If _User.Length > 0 OrElse _DB.Length > 0 OrElse _Host.Length > 0 OrElse _MyProcessID.ToString.Length > 0 Then
            _MyQuery.SQL.AppendLine("where 1=1")
        End If
        If _User.Length > 0 Then
            _MyQuery.SQL.AppendLine("and USER LIKE ?user")
            _MyQuery.DeclareAndSet("user", "%" & _User & "%")
        End If
        If _DB.Length > 0 Then
            _MyQuery.SQL.AppendLine("and DB like ?db")
            _MyQuery.DeclareAndSet("db", "%" & _DB & "%")
        End If
        If _Host.Length > 0 Then
            _MyQuery.SQL.AppendLine("and host like ?host")
            _MyQuery.DeclareAndSet("host", "%" & _Host & "%")
        End If
        If _IgnoreMySelf Then
            _MyQuery.SQL.AppendLine("and ID!=?id")
            _MyQuery.DeclareAndSet("id", _MyProcessID)
        End If
        _MyQuery.SQL.AppendLine("order by")
        _MyQuery.SQL.AppendLine(" id")

    End Sub

    Public Sub Refresh()
        _List.Clear()
        _MyQuery.Execute()
        While Not _MyQuery.Eof
            Dim item As New TMyProcess
            item._Id = _MyQuery.FieldAsInteger("ID")
            item._Host = _MyQuery.FieldAsString("HOST")
            item._User = _MyQuery.FieldAsString("USER")
            item._DB = _MyQuery.FieldAsString("DB")
            item._Command = _MyQuery.FieldAsString("COMMAND")
            item._Time = _MyQuery.FieldAsInteger("TIME")
            item._State = _MyQuery.FieldAsString("STATE")
            item._Info = _MyQuery.FieldAsString("INFO")
            _List.Add(item)
            _MyQuery.MoveNext()
        End While
        _MyQuery.Close()
    End Sub

    Public ReadOnly Property DataTable() As DataTable
        Get
            Return _MyQuery.DataSet.Tables(0).Copy
        End Get
    End Property

    Private disposedValue As Boolean = False        ' 重複する呼び出しを検出するには

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 他の状態を解放します (マネージ オブジェクト)。
            End If
            _MyQuery.Close()
            ' TODO: ユーザー独自の状態を解放します (アンマネージ オブジェクト)。
            ' TODO: 大きなフィールドを null に設定します。
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' このコードを変更しないでください。クリーンアップ コードを上の Dispose(ByVal disposing As Boolean) に記述します。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
